home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9259 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: brtph500.bnr.ca!oophelp
  2. From: oophelp@bnr.ca (OOP Helpline)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: polymorphism and style
  5. Date: 29 Feb 1996 15:35:13 GMT
  6. Organization: Bell Northern Research
  7. Message-ID: <4h4h3h$6ur@brtph500.bnr.ca>
  8. References: <4gr4ur$n13@alpha.pcix.com> <DnI6II.KMt@news.arco.com>
  9. NNTP-Posting-Host: brtph89d.bnr.ca
  10. Originator: oophelp@brtph89d
  11.  
  12.  
  13. |> >  Question on polymorphism and style.
  14. |> >Want to call a function in a most derived class off a pointer to the base
  15. |> >class. The fuction does not exist in the base class.
  16. |> >I should:
  17. |> >1)downcast ?
  18. |> >2)add the function to the base class and add virtual stubs to every 
  19. |> >  class that does not use it ? 
  20. |> >3)add it to the base class but define it like 
  21. |> >     foo( ..whatever..) {error_msg("virtual function not defined");}
  22. |> >  This way only the class that uses it would have to define it.
  23. |> >4) ?
  24. |> > 
  25. |> >thanks for your time.
  26.  
  27. |>but maybe you need to take another
  28. |> look at your design.
  29.  
  30. Exactly, this is clearly a design fault. Upcasting a derived class object
  31. to a base class requires that you only do base class operations on it.
  32. (While any pilot can be a passenger on an airplane, any passenger cannot
  33. be a pilot.)
  34.  
  35. If you need to keep the ability to do derived class behavior on a derived
  36. class object, you need to keep a derived class reference to it
  37. (i.e. use two pointers to the derived class object, one for use
  38. for base class behavior and one for use for derived class behavior).
  39.